package com.dap.batch.controller;

import com.dap.batch.dao.IDaoService;
import com.dap.batch.entity.Result;
import com.dap.batch.enums.ResultEnum;
import com.dap.batch.exception.BatchCustomException;
import com.dap.batch.po.BatchObjectExecutionPo;
import com.dap.batch.po.BatchStepExecutionPo;
import com.dap.batch.utils.ResultUtils;
import com.dap.dao.model.QueryResult;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2017/9/28 15:59
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/9/28 15:59
 */
@RestController
@RequestMapping("/v1/step")
public class BatchStepMonitorController {

    private final static Logger logger = LoggerFactory.getLogger(BatchStepMonitorController.class);

    private IDaoService daoService;

    @Autowired
    public BatchStepMonitorController(IDaoService daoService) {
        this.daoService = daoService;
    }

    /**
     * 查询所有监控数据
     *
     * @param jobExeId
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "获取当前任务下步骤监控信息", httpMethod = "GET")
    @GetMapping("/monitors/{jobExeId}")
    public Result list(@PathVariable Long jobExeId
            , @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit) {
        BatchStepExecutionPo po = new BatchStepExecutionPo();
        po.setJobExeId(jobExeId);
        QueryResult<BatchStepExecutionPo> result = daoService.selectQueryResult(po,
                page, limit);
        if (result.getTotalrecord() == 0) {
            throw new BatchCustomException(ResultEnum.NOT_FOUND);
        }
        return ResultUtils.success(result);
    }

    /**
     * 查询当前step所有执行对象
     * @param stepExeId
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "获取当前步骤下对象监控信息", httpMethod = "GET")
    @GetMapping("/monitor/objects/{stepExeId}")
    public Result listObject(@PathVariable Long stepExeId
            , @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit) {
        BatchObjectExecutionPo po = new BatchObjectExecutionPo();
        po.setStepExeId(stepExeId);
        QueryResult<BatchObjectExecutionPo> result = daoService.selectQueryResult(po,
                page, limit);
        if (result.getTotalrecord() == 0) {
            throw new BatchCustomException(ResultEnum.NOT_FOUND);
        }
        return ResultUtils.success(result);
    }

    /**
     * 修改执行参数
     * @param po
     * @return
     */
    @ApiOperation(value = "修改步骤执行参数", httpMethod = "PUT")
    @PutMapping("/monitor")
    public Result update(BatchStepExecutionPo po) {
        int i = daoService.update(po);
        if (i == 0) {
            throw new BatchCustomException(ResultEnum.NOT_FOUND);
        }
        return ResultUtils.success(ResultEnum.CREATED);
    }

}
